<?php 
class SysRole extends base_model 
{
	const tablename = 'sysrole';
	var $roleid;
	var $rolename;
	var $isactive=0;

		
	public function __construct($mid='')
	{					
		if($mid!='')
			$this->LookUp($mid);
	}

	function Update()
	{
		
		$this->UpdateBy=$this->loginuser['uname'];
		$this->UpdateDate=date('Y-m-d H:i:s');		
		$this->db->where('roleid', $this->roleid);
		return $this->db->update($this->db->dbprefix($this::tablename), $this); 
	}
	
	function Create($id)
	{	
		$this->CreatedBy=$this->loginuser['uname'];
		$this->Created=date('Y-m-d H:i:s');
		return $this->db->insert($this->db->dbprefix($this::tablename), $this); 
	}

	function LookUp($roleid)
	{	
		$rw= $this->Inquiry($roleid);	
		
		if(count($rw)>0){				
			$this->roleid=$rw['roleid'];
			$this->rolename=$rw['rolename'];
			$this->isactive=$rw['isactive'];			
			$this->parentmap($rw);
		}
		else{
			return false;
		}
	}

	function Delete($id)
	{
		$this->db->where_in('roleid', $id);
		return $this->db->delete($this->db->dbprefix($this::tablename)); 		
	}	

	public function Inquiry($roleid = false,$order=false)
	{	
		
		if($roleid)
		 $this->db->where('roleid',$roleid);		
		
		if($order)
		$this->db->order_by($order);

		$query = $this->db->get($this->db->dbprefix($this::tablename));		
		
				
		if ($roleid === false)
		{			
					
			return $query->result_array();
		}
		else
		{			
			return $query->row_array();
		}
	} 


	public function getTable()
    {
        /* Array of database columns which should be read and sent back to DataTables. Use a space where
         * you want to insert a non-database field (for example a counter or static image)
         */
        $aColumns = array('RoleId', 'RoleName', 'IsActive');
        
        // DB table to use
        $sTable = $this->db->dbprefix($this::tablename);
        //
    
        $iDisplayStart = $this->input->get_post('iDisplayStart', true);
        $iDisplayLength = $this->input->get_post('iDisplayLength', true);
        $iSortCol_0 = $this->input->get_post('iSortCol_0', true);
        $iSortingCols = $this->input->get_post('iSortingCols', true);
        $sSearch = $this->input->get_post('sSearch', true);
        $sEcho = $this->input->get_post('sEcho', true);
    
        // Paging
        if(isset($iDisplayStart) && $iDisplayLength != '-1')
        {
            $this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
        }
        
        // Ordering
        if(isset($iSortCol_0))
        {
            for($i=0; $i<intval($iSortingCols); $i++)
            {
                $iSortCol = $this->input->get_post('iSortCol_'.$i, true);
                $bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
                $sSortDir = $this->input->get_post('sSortDir_'.$i, true);
    
                if($bSortable == 'true')
                {
                    $this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
                }
            }
        }
        
        /* 
         * Filtering
         * NOTE this does not match the built-in DataTables filtering which does it
         * word by word on any field. It's possible to do here, but concerned about efficiency
         * on very large tables, and MySQL's regex functionality is very limited
         */
        if(isset($sSearch) && !empty($sSearch))
        {
            for($i=0; $i<count($aColumns); $i++)
            {
                $bSearchable = $this->input->get_post('bSearchable_'.$i, true);
                
                // Individual column filtering
                if(isset($bSearchable) && $bSearchable == 'true')
                {
                    $this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
                }
            }
        }
        
        // Select Data
        $this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
        $rResult = $this->db->get($sTable);
    
        // Data set length after filtering
        $this->db->select('FOUND_ROWS() AS found_rows');
        $iFilteredTotal = $this->db->get()->row()->found_rows;
    
        // Total data set length
        $iTotal = $this->db->count_all($sTable);
    
        // Output
        $output = array(
            'sEcho' => intval($sEcho),
            'iTotalRecords' => $iTotal,
            'iTotalDisplayRecords' => $iFilteredTotal,
            'aaData' => array()
        );
        
        foreach($rResult->result_array() as $aRow)
        {
            $row = array();
            
            foreach($aColumns as $col)
            {
                $row[] = $aRow[$col];
            }
    
            $output['aaData'][] = $row;
        }
		
        return $output;
    }

}



?>



